package db.document;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import db.Database;
import po.AccountBookPo;
import po.DocumentPo;
import po.Po;
import po.库存报警单po;


public class StockWarningDocumentDB extends DocumentDB{
	
	
	
	public StockWarningDocumentDB(AccountBookPo ab) {
		super(ab);
	}
	public Po add(DocumentPo document) {
		return null;
	
	}

	@Override
	public Po update(DocumentPo document) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DocumentPo> getUnapproval() {
		List<DocumentPo> list=new ArrayList<DocumentPo>();
		Connection c=Database.getConnection();
		Statement s;int total_Num=0;
		try {
			s = c.createStatement();
			ResultSet rs=s.executeQuery("select f_commodityID,f_name,f_model,f_stockNum from "+book.getName()+"_commodity where f_stockNum<=f_dangerNum ");
			while(rs.next()){
				库存报警单po po=new 库存报警单po();
				po.set单据编号(getDocumentId("KCBJD",total_Num));
				po.set商品编号(rs.getString(1));
				po.set商品名称(rs.getString(2));
				po.set商品型号(rs.getString(3));
				po.setNum(rs.getInt(4));
				list.add(po);
				total_Num++;
			}
	        s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<DocumentPo> get(DocumentPo from, DocumentPo to) {
		List<DocumentPo> list=new ArrayList<DocumentPo>();
		String from_date;String to_date;
		from_date=from.getDate()==null? null:new SimpleDateFormat("yyyy-MM-dd").format(from.getDate());
		to_date=to.getDate()==null?null:new SimpleDateFormat("yyyy-MM-dd").format(to.getDate());
		String query_from=from_date==null? "":" f_date>='"+from_date+"'";
		String query_to=to_date==null? "":"and f_date<='"+to_date+"'";
		Connection c=Database.getConnection();
		Statement s;ResultSet rs = null;
		try {
			s = c.createStatement();
			rs=s.executeQuery("select * from "+book.getName()+"_Doc_stock_danger where "+query_from+query_to);
			while(rs.next()){
				库存报警单po po=new 库存报警单po();
				po.set单据编号(rs.getString(1));
				po.set商品编号(rs.getString(2));
				po.set商品名称(rs.getString(3));
				po.set商品型号(rs.getString(4));
				po.setNum(rs.getInt(5));
				list.add(po);
		   }
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Po approve(DocumentPo document) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
